package cn.task.part2.part3.window;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cn.task.part2.part3.dao.PreferredCustomer;
import cn.task.part2.part3.utils.Entry;

public class PreferredWindow extends JPanel implements ActionListener{
	JLabel[] labels;
	String[] labelNames={"姓名:","地址:","电话:","编号:","邮件:","采购:"};
	JTextField nameString,addressString,telephoneString,numberString,isMailString,spendsString;
	
	
	JButton inputButton,resetButton;
	
	public void init(){
		Font labelFont=new Font("宋体",Font.PLAIN,20); //标签字体
		labels=new JLabel[labelNames.length];
		for(int i=0;i<labelNames.length;i++){
			labels[i]=new JLabel(labelNames[i],JLabel.CENTER);
			labels[i].setFont(labelFont);
		}
		
		spendsString=new JTextField(20);
		spendsString.setFont(labelFont);
		
		isMailString=new JTextField(20);
		isMailString.setFont(labelFont);
		
		numberString=new JTextField(20);
		numberString.setFont(labelFont);
		
		telephoneString=new JTextField(20);
		telephoneString.setFont(labelFont);
		
		nameString=new JTextField(20);
		nameString.setFont(labelFont);
		
		addressString=new JTextField(20);
		addressString.setFont(labelFont);
		
		inputButton=new JButton("录入");
		resetButton=new JButton("重置");
		
		Box[] horiBoxes=new Box[labelNames.length];
		for(int i=0;i<labelNames.length;i++){
			horiBoxes[i]=Box.createHorizontalBox();
			horiBoxes[i].add(labels[i]);
		}
		horiBoxes[0].add(nameString);
		horiBoxes[1].add(addressString);
		horiBoxes[2].add(telephoneString);
		horiBoxes[3].add(numberString);
		horiBoxes[4].add(isMailString);
		horiBoxes[5].add(spendsString);
		
		Box verticalBox=Box.createVerticalBox();
		for(int i=0;i<labelNames.length;i++){
			verticalBox.add(horiBoxes[i]);
			verticalBox.add(Box.createVerticalStrut(15));
		}
		
		JPanel centerPanel=new JPanel();
		centerPanel.setOpaque(false);
		centerPanel.add(verticalBox);
		JPanel buttonPanel =new JPanel();
		buttonPanel.add(inputButton);
		buttonPanel.add(resetButton);
		inputButton.addActionListener(this);
		resetButton.addActionListener(this);
		
		//设置边界式布局
		this.setLayout(new BorderLayout());
		this.add(centerPanel,BorderLayout.CENTER);
		this.add(buttonPanel,BorderLayout.SOUTH);
	}
	public PreferredWindow(){
		init();
	}
	
	
	public boolean isLegal(String str){//判断文本框内的内容是否非空
		if(str==null || str.equals("") || str.contains(" "))
			return false;
		return true;
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getActionCommand().equals("重置")){//点击了重置按钮
			addressString.setText("");
			nameString.setText("");
			telephoneString.setText("");
			numberString.setText("");
			isMailString.setText("");
		}else {//点击了录入按钮
			if(!isLegal(addressString.getText())) {
				JOptionPane.showMessageDialog(this, "您输入的地址不规范：地址为空或包含空格！",
						"地址非法",JOptionPane.OK_OPTION);
				addressString.requestFocus();
				addressString.selectAll();
			}else if(!isLegal(nameString.getText())){
				JOptionPane.showMessageDialog(this, "您输入的姓名不规范：姓名为空或包含空格！",
						"姓名非法",JOptionPane.OK_OPTION);
				nameString.requestFocus();
				nameString.selectAll();
			}else if(!isLegal(telephoneString.getText())) {
				JOptionPane.showMessageDialog(this, "您输入的电话不规范：电话为空或包含空格！",
						"电话非法",JOptionPane.OK_OPTION);
				telephoneString.requestFocus();
				telephoneString.selectAll();
			}else if(!isLegal(numberString.getText())) {
				JOptionPane.showMessageDialog(this, "您输入的顾客编号不规范：顾客编号为空或包含空格！",
						"顾客编号非法",JOptionPane.OK_OPTION);
				numberString.requestFocus();
				numberString.selectAll();
			}else if(!isLegal(isMailString.getText())) {
				JOptionPane.showMessageDialog(this, "您输入的是否在邮件列表不规范：是否在邮件列表为空或包含空格！",
						"是否在邮件列表非法",JOptionPane.OK_OPTION);
				isMailString.requestFocus();
				isMailString.selectAll();
			}else if(!isLegal(spendsString.getText())) {
				JOptionPane.showMessageDialog(this, "您输入的采购不规范：采购为空或包含空格！",
						"采购非法",JOptionPane.OK_OPTION);
				spendsString.requestFocus();
				spendsString.selectAll();
			}
			else {
				String name = nameString.getText();
				String address = addressString.getText();
				String telephone = telephoneString.getText();
				String number = numberString.getText();
				String isMail = isMailString.getText();
				Boolean trueIsMail = null;
				if(isMail.equals("true")) {
					trueIsMail = true;
				}else if(isMail.equals("false")) {
					trueIsMail = false;
				}
				Integer spends =  Integer.valueOf(spendsString.getText());
				PreferredCustomer pc = new PreferredCustomer(name, address, telephone, number, trueIsMail, spends);
				Entry.createPreferred(pc);
				JOptionPane.showMessageDialog(this, "您插入顾客信息成功",
						"录入提示",JOptionPane.PLAIN_MESSAGE);
			}
		}
		
	}
}
